package edu.neumont.MillerK.POJOs;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class Security {
	private static Random rand;
	
	public Security(){
		rand = new Random();
	}
	
	//SHA-256 hashing algorithm
	public String SHAHash(String saltedPass) throws NoSuchAlgorithmException{
		String saltedPassword = saltedPass;
		 
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(saltedPassword.getBytes());
 
        byte byteData[] = md.digest();
 
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
         sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }
        
        return sb.toString();
	}
	
	public String generateSalt(int saltSize){
		String salt = "";
		char[] saltChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
		for(int count = 0; count < saltSize; count++){
			salt += saltChars[rand.nextInt(saltChars.length)];
		}
		return salt;
	}
}
